import 'dart:convert';

extension StringExtension on String? {
  String toAssets() {
    return "assets/images/${this}";
  }

  String toCharacterBreakStr() {
    /// 将flutter系统默认的单词截断模式转换成字符截断模式
    /// 通过向文本中插入宽度为0的空格实现
    if (!(this?.isNotEmpty == true)) {
      return this ?? "";
    }
    String breakWord = '';
    this!.runes.forEach((element) {
      breakWord += String.fromCharCode(element);
      breakWord += '\u200B';
    });
    return breakWord;
  }

  String toNoBreakStr() {
    /// 通过向文本中插入宽度为0的空格实现不折行
    if (!(this?.isNotEmpty == true)) {
      return this ?? "";
    }
    String breakWord = '';
    this!.runes.forEach((element) {
      breakWord += String.fromCharCode(element);
      breakWord += '\ufeff';
    });
    return breakWord;
  }

  ///根据状态改变点赞或者收藏数
  String toNumChangeFormat(bool? status) {
    int? collectInt = int.tryParse(this ?? "");
    if (collectInt is int) {
      return (status ?? false)
          ? (collectInt + 1).toString()
          : (collectInt - 1).toString();
    }
    return this ?? "";
  }

  int? toInt() {
    if (this is String) {
      int? tmp = int.tryParse(this!);
      return tmp;
    } else {
      return null;
    }
  }

  double? toDouble() {
    if (this is String) {
      double? tmp = double.tryParse(this!);
      return tmp;
    } else {
      return null;
    }
  }

  // 将手机号中间四位隐藏
  String toPhoneNumber() {
    if (this is String) {
      if (this == null || this!.isEmpty) {
        return '';
      }
      // return this!.replaceRange(3, 7, '****');
      final int maskLength = 4;
      final String maskChar = '*';
      String startDigits = this!.substring(0, this!.length - maskLength * 2);
      // Get the last four digits of phone number
      String lastFourDigits = this!.substring(this!.length - maskLength);
      // Generate masked string
      String maskedString = startDigits;
      for (int i = this!.length - maskLength - 4;
          i < this!.length - maskLength;
          i++) {
        maskedString += maskChar;
      }
      // Combine masked string and last four digits
      maskedString += lastFourDigits;
      return maskedString;
    } else {
      return "";
    }
  }

  /*
  * 10.0 转 10；10.5还是返回10.5
  * */
  String toFormatNumber() {
    if (this == null) {
      return "";
    }
    if (this!.contains(".") == true) {
      List<String> list = this!.split(".");
      if (list.length == 2) {
        String last = list.last;
        if (last == "0" || last == "00") {
          return list.first;
        }
      }
    }
    return this ?? "";
  }

  Map<String, dynamic> toJsonData() {
    if (this?.isEmpty == true) {
      return {};
    }

    // 解析查询字符串
    Map<String, String> queryParams = {};
    List<String> pairs = this!.split('&');
    for (String pair in pairs) {
      List<String> keyValue = pair.split('=');
      if (keyValue.length == 2) {
        String key = keyValue[0];
        String value = keyValue[1];
        queryParams[key] = value;
      }
    }
    // 将 Map 转换为 JSON 字符串
    String jsonString = json.encode(queryParams);
    print('JSON 字符串: $jsonString');
    // 解析 JSON 字符串
    Map<String, dynamic> jsonMap = json.decode(jsonString);
    print('JSON 映射: $jsonMap');
    return jsonMap;
  }
}
